//
//  MineTopView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/24.
//

#import "MineTopView.h"

@interface MineTopView()

@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UIButton *editBtn;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *idLabel;
@property(nonatomic,strong)UILabel *signLabel;
@property(nonatomic,strong)UIView *bgView;

@end

@implementation MineTopView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = [UIColor clearColor];
    
    self.headImageView = [[UIImageView alloc]init];
    self.headImageView.layer.cornerRadius = 68/2.0;
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.headImageView.layer.borderWidth = 3.0;
    self.headImageView.image = [UIImage imageNamed:@"mine_head_default"];
    [self addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@68);
        make.left.mas_equalTo(@16);
        make.top.mas_equalTo(@0);
    }];
    
    self.signLabel = [[UILabel alloc]init];
    self.signLabel.font = JPUIFontPingFangSCMedium(13);
    self.signLabel.textColor = KFontBlackColor;
    self.signLabel.numberOfLines = 0;
    [self addSubview:self.signLabel];
    [self.signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.top.mas_equalTo(self_weak_.headImageView.mas_bottom).offset(16);
    }];
    self.signLabel.hidden = YES;
    
    self.loginBtn = [[UIButton alloc]init];
    [self.loginBtn setTitle:@"点击登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:KFontBlackColor forState:UIControlStateNormal];
    self.loginBtn.titleLabel.font = JPUIFontPingFangSCMedium(20);
    [self.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.loginBtn];
    CGFloat login_w = [self getStringWidthWithText:self.loginBtn.titleLabel.text font:self.loginBtn.titleLabel.font viewHeight:34];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.headImageView.mas_right).offset(13);
        make.width.mas_equalTo(login_w+6);
        make.height.mas_equalTo(@34);
        make.top.mas_equalTo(self_weak_.headImageView.mas_top);
    }];
    
    self.tipLabel = [[UILabel alloc]init];
    self.tipLabel.font = JPUIFontPingFangSCRegular(16);
    self.tipLabel.textColor = KFontGrayColor;
    self.tipLabel.text = @"今天又是元气满满的一天～";
    [self addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.headImageView.mas_right).offset(16);
        make.height.mas_equalTo(@34);
        make.bottom.mas_equalTo(self_weak_.headImageView.mas_bottom);
        make.right.mas_equalTo(@-16);
    }];
    
    self.editBtn = [[UIButton alloc]init];
    [self.editBtn setTitle:@"编辑资料" forState:UIControlStateNormal];
    [self.editBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    self.editBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    [self.editBtn setImage:[UIImage imageNamed:@"mine_right_arrow"] forState:UIControlStateNormal];
    [self.editBtn setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
    [self.editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.editBtn];
    CGFloat edit_w = [self getStringWidthWithText:self.editBtn.titleLabel.text font:self.editBtn.titleLabel.font viewHeight:34];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-16);
        make.width.mas_equalTo(edit_w+16);
        make.centerY.mas_equalTo(self_weak_.headImageView);
        make.height.mas_equalTo(@34);
    }];
    
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.textColor = KFontBlackColor;
    self.nameLabel.font  =JPUIFontPingFangSCMedium(20);
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.headImageView.mas_right).offset(16);
        make.height.mas_equalTo(@34);
        make.top.mas_equalTo(self_weak_.headImageView.mas_top);
        make.right.mas_equalTo(self_weak_.editBtn.mas_left).offset(-10);
    }];
    
    self.idLabel = [[UILabel alloc]init];
    self.idLabel.textColor = KFontGrayColor;
    self.idLabel.font  = JPUIFontPingFangSCRegular(16);
    [self addSubview:self.idLabel];
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self_weak_.headImageView.mas_right).offset(16);
        make.height.mas_equalTo(@34);
        make.bottom.mas_equalTo(self_weak_.headImageView.mas_bottom);
        make.right.mas_equalTo(self_weak_.editBtn.mas_left).offset(-10);
    }];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-16);
        make.height.mas_equalTo(@90);
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
    }];
    self.bgView = bgView;
    
    
    [self createItemView];
    
    
}

-(void)createItemView{
    CGFloat currentAvailableWidth = [DeviceUtils currentAvailableWidth];
    __weak typeof(self)self_weak_ = self;
    CGFloat item_w = (currentAvailableWidth-32-20)/3.0;
    NSArray *titleArr = @[@"学习日历",@"我的岛币",@"我的收藏"];
    for(NSInteger i = 0;i < titleArr.count;i ++){
        UIView *itemView = [[UIView alloc]init];
        itemView.backgroundColor = [UIColor whiteColor];
        [self.bgView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((item_w+10)*i);
            make.width.mas_equalTo(item_w);
            make.height.mas_equalTo(70);
            make.centerY.mas_equalTo(self_weak_.bgView);
        }];
        UITapGestureRecognizer *item_tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            if(self_weak_.cateBlock){
                self_weak_.cateBlock(i);
            }
        }];
        [itemView addGestureRecognizer:item_tap];
        UIImageView *itemLogoImageView = [[UIImageView alloc]init];
        itemLogoImageView.userInteractionEnabled = YES;
        itemLogoImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"mine_top_cate_logo%ld",i+1]];
        [itemView addSubview:itemLogoImageView];
        [itemLogoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(@24);
            make.top.mas_equalTo(@10);
            make.centerX.mas_equalTo(itemView);
        }];
        UILabel *itemLabel = [[UILabel alloc]init];
        itemLabel.font = JPUIFontPingFangSCMedium(13);
        itemLabel.textColor = KFontBlackColor;
        itemLabel.text = titleArr[i];
        itemLabel.textAlignment = NSTextAlignmentCenter;
        itemLabel.userInteractionEnabled = YES;
        [itemView addSubview:itemLabel];
        [itemLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@5);
            make.right.mas_equalTo(@-5);
            make.top.mas_equalTo(itemLogoImageView.mas_bottom).offset(8);
        }];
        
    }
    
}

-(void)refreshWidth{
    [self.bgView removeAllSubviews];
    [self createItemView];
}

#pragma mark - 登录
-(void)loginBtnClick{
    if(_loginBlock){
        _loginBlock();
    }
}

#pragma mark - 编辑
-(void)editBtnClick{
    if(_editBlock){
        _editBlock();
    }
}

-(void)setUserModel:(MineUserModel *)userModel{
    _userModel = userModel;
    if(userModel.userId){
        self.loginBtn.hidden = YES;
        self.tipLabel.hidden = YES;
        self.nameLabel.hidden = NO;
        self.idLabel.hidden = NO;
        self.editBtn.hidden = NO;
        self.idLabel.text = [NSString stringWithFormat:@"ID：%@",userModel.userId];
        if(userModel.avatar){
            [self.headImageView sd_setImageWithURL:[NSURL URLWithString:userModel.avatar] placeholderImage:[UIImage imageNamed:@"mine_head_default"]];
        }else{
            self.headImageView.image = [UIImage imageNamed:@"mine_head_default"];
        }
        if(userModel.nickname){
            self.nameLabel.text = userModel.nickname;
        }else{
            if(userModel.phone){
                self.nameLabel.text = [self hideMiddleDigits:userModel.phone];
            }else{
                self.nameLabel.text = @"日语岛民";
            }
        }
        if(userModel.introduction&&![userModel.introduction isEqualToString:@""]){
            self.signLabel.hidden = NO;
            self.signLabel.text = userModel.introduction;
        }else{
            self.signLabel.hidden = YES;
        }
    }else{
        self.signLabel.hidden = YES;
        self.loginBtn.hidden = NO;
        self.tipLabel.hidden = NO;
        self.nameLabel.hidden = YES;
        self.idLabel.hidden = YES;
        self.editBtn.hidden = YES;
        self.headImageView.image = [UIImage imageNamed:@"mine_head_default"];
    }
}

-(NSString*)hideMiddleDigits:(NSString*)phoneNumber{
     NSString *maskedNumber = phoneNumber;
     if (phoneNumber.length == 13||phoneNumber.length == 11) {
         // 中国手机号码 86+11位
         // 台湾手机号码 886 +10 位
         // 香港、澳门手机号码 852+8位 853+8位
         // 日本手机号码 81+11位
         NSRange range = NSMakeRange(5,4);
         maskedNumber = [phoneNumber stringByReplacingCharactersInRange:range withString:@"****"];
     }else if (phoneNumber.length == 12) {
         // 香港手机号码 852+9位
         NSRange range = NSMakeRange(6,4);
         maskedNumber = [phoneNumber stringByReplacingCharactersInRange:range withString:@"****"];
     }
     return maskedNumber;
}




@end
